package top.lyc829.zzl_android.ui.message.praise;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.listener.OnItemSwipeListener;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.gson.Gson;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Response;
import top.lyc829.zzl_android.R;
import top.lyc829.zzl_android.databinding.ActivityNoticePraiseBinding;
import top.lyc829.zzl_android.logic.bean.CommentBean;
import top.lyc829.zzl_android.logic.bean.NoticeCommentBean;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.bean.SuperCommentBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.PageInfo;
import top.lyc829.zzl_android.logic.util.ReplayType;
import top.lyc829.zzl_android.logic.util.Url;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;
import top.lyc829.zzl_android.ui.base_detail.BaseDetailActivity;
import top.lyc829.zzl_android.ui.base_detail.SonCommentsAdapter;
import top.lyc829.zzl_android.ui.custom_view.InputTextMsgDialog;
import top.lyc829.zzl_android.ui.message.replay.ReplayActivity;
import top.lyc829.zzl_android.ui.message.replay.ReplayAdapter;
import top.lyc829.zzl_android.ui.message.replay.ReplayViewModel;
import top.lyc829.zzl_android.ui.profile.follow.user_detail.OtherUserDetailActivity;
import top.lyc829.zzl_android.ui.thing.dynamic_detail.DynamicDetailActivity;
import top.lyc829.zzl_android.ui.thing.travel_detail.TravelDetailActivity;

public class NoticePraiseActivity extends AppCompatActivity {

    ActivityNoticePraiseBinding binding;
    NoticePraiseViewModel mViewModel;
    NoticePraiseAdapter adapter;

    MaterialDialog loadingDialog;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityNoticePraiseBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.mTitleBarNoticePraise.setLeftClickListener(v -> finish());
        mViewModel = new ViewModelProvider.AndroidViewModelFactory(getApplication()).create(NoticePraiseViewModel.class);


        // 实例化加载框
        loadingDialog = new MaterialDialog.Builder(this)
                .customView(R.layout.dialog_loading_layout, true)
                .cancelable(false)
                .build();


        initRecycler();

        observer();

        clickListener();

    }

    private void clickListener() {
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            NoticeCommentBean comment = adapter.getItem(position);
            Intent intent;

            if (comment.getSourceType().equals(ReplayType.BASE)) {
                intent = new Intent(NoticePraiseActivity.this, BaseDetailActivity.class);
                startActivity(intent);
            } else if (comment.getSourceType().equals(ReplayType.DYNAMIC)) {
                intent = new Intent(NoticePraiseActivity.this, DynamicDetailActivity.class);
                intent.putExtra("id", comment.getSourceId());
                startActivity(intent);
            } else if (comment.getSourceType().equals(ReplayType.COMMENT)) {
                HashMap<String, String> body = new HashMap<>();
                body.put("comment_id", comment.getSourceId().toString());
                HttpUtil.getInstance(this).get(Url.GET_TOP_COMMENT_BY_N, body, new HttpUtil.MyCallback() {
                    @Override
                    public void success(Response res) throws IOException {
                        String resStr = res.body().string();
                        ResultBean<CommentBean> resultBean = JsonUtilNew.fromJsonObject(resStr, CommentBean.class);
                        if (resultBean.getCode().equals(200)) {
                            runOnUiThread(() -> {
                                goToSourcePageByComment(resultBean.getData());
                            });
                        } else {
                            failed(new IOException(resultBean.getMsg()));
                        }
                    }

                    @Override
                    public void failed(IOException e) {
                        Utils.SoutErr(e.getMessage());
                    }
                });

            } else if (comment.getSourceType().equals(ReplayType.TRAVEL)) {
                intent = new Intent(NoticePraiseActivity.this, TravelDetailActivity.class);
                intent.putExtra("travel_id", comment.getSourceId());
                startActivity(intent);
            }

        });

        adapter.addChildClickViewIds(R.id.notice_praise_item_avatar);
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {
            if (view.getId() == R.id.notice_praise_item_avatar) {
                Intent intent = new Intent(NoticePraiseActivity.this, OtherUserDetailActivity.class);
                intent.putExtra("id", adapter.getItem(position).getUserId());
                startActivity(intent);
            }
        });

    }

    // 通过n级评论通知跳转到具体的界面
    private void goToSourcePageByComment(CommentBean comment) {
        Intent intent = null;
        if (comment.getType().equals(ReplayType.BASE)) {
            intent = new Intent(NoticePraiseActivity.this, BaseDetailActivity.class);
            intent.putExtra("is_comment", true);
            intent.putExtra("id", comment.getToId());
            startActivity(intent);
        } else if (comment.getType().equals(ReplayType.DYNAMIC)) {
            intent = new Intent(NoticePraiseActivity.this, DynamicDetailActivity.class);
            intent.putExtra("id", comment.getToId());
            intent.putExtra("is_comment", true);
            //intent.putExtra("", comment.getToId());
            startActivity(intent);
        } else if (comment.getType().equals(ReplayType.TRAVEL)) {
            intent = new Intent(NoticePraiseActivity.this, TravelDetailActivity.class);
            intent.putExtra("travel_id", comment.getToId());
            intent.putExtra("is_comment", true);
            startActivity(intent);
        }
    }

    private void observer() {

        mViewModel.getNoticePraisesLiveData().observe(this, noticeCommentBeans -> {
            adapter.setList(noticeCommentBeans);
        });

    }

    private void initRecycler() {

        // 侧滑监听
        OnItemSwipeListener onItemSwipeListener = new OnItemSwipeListener() {
            NoticeCommentBean commentBean = null;

            @Override
            public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {
                commentBean = adapter.getItem(pos);
            }

            @Override
            public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {
            }

            @Override
            public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {
                Utils.Sout("View Swiped: " + pos);
                mViewModel.deleteItem(commentBean);
            }

            @Override
            public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {
                canvas.drawColor(ContextCompat.getColor(NoticePraiseActivity.this, R.color.zzl_red));
            }
        };

        adapter = new NoticePraiseAdapter();
        binding.noticePraiseRecycler.setLayoutManager(new LinearLayoutManager(this));
        adapter.getDraggableModule().setSwipeEnabled(true);
        adapter.getDraggableModule().setOnItemSwipeListener(onItemSwipeListener);
        adapter.getDraggableModule().getItemTouchHelperCallback().setSwipeMoveFlags(ItemTouchHelper.START | ItemTouchHelper.END);
        binding.noticePraiseRecycler.setAdapter(adapter);
        adapter.setAnimationEnable(true);   // recyclerview加载动画
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!Utils.isLogin()) {
            Utils.showToast("请先登录");
            //Utils.goLogin(this);
        } else {
            mViewModel.loadNoticePraises(Utils.getUserId());
        }
    }
}